package com.laikan.reader.bookreader.activity;

import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.laikan.library.components.activitys.BasicActivity;
import com.laikan.library.httpvolley.engine.HttpApiRequestEngine;
import com.laikan.library.httpvolley.engine.Requester;
import com.laikan.library.httpvolley.helper.RequestListener;
import com.laikan.library.httpvolley.helper.VolleyErrorHandler;
import com.laikan.library.httpvolley.response.HttpResponse;
import com.laikan.library.managers.ToastMgr;
import com.laikan.library.utils.SPUtil;
import com.laikan.reader.R;
import com.laikan.reader.comment.APIProtocol;
import com.laikan.reader.comment.Constants;
import com.socks.library.KLog;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * 投票页
 *
 * @author wxl
 */
public class VoteActivity extends BasicActivity implements OnClickListener {

    private TextView back_tv, vote_button_gold, vote_button_tj, vote_button_qd, book_name;

    private String bookname, bookid, type;
    private TextView ranking, tj_num, qd_num;
    private boolean isGold, isOver;
    private String qdp, tjp, goldNum;
    private RelativeLayout qdp_rel, gold_rel;
    private ImageView back;
    private int count = 0;
    private String userGoldMatchCount = "", userSupportCount = "";
    private int userGoldMatch = 0, userSupport = 0;

    @Override
    protected void assignViews() {
        setContentView(R.layout.lk_vote);
        back = (ImageView)findViewById(R.id.back);
        gold_rel = (RelativeLayout)findViewById(R.id.gold_rel);
        qdp_rel = (RelativeLayout)findViewById(R.id.qdp_rel);
        ranking = (TextView)findViewById(R.id.ranking);
        tj_num = (TextView)findViewById(R.id.tj_num);
        qd_num = (TextView)findViewById(R.id.qd_num);
        book_name = (TextView)findViewById(R.id.book_name);
        vote_button_gold = (TextView)findViewById(R.id.vote_button_gold);
        vote_button_tj = (TextView)findViewById(R.id.vote_button_tj);
        vote_button_qd = (TextView)findViewById(R.id.vote_button_qd);
        back_tv = (TextView)findViewById(R.id.iv_back);
    }

    @Override
    protected void initData() {
        userGoldMatchCount = getIntent().getStringExtra("userGoldMatchCount");
        userSupportCount = getIntent().getStringExtra("userSupportCount");
        goldNum = getIntent().getStringExtra("goldNum");
        isGold = getIntent().getBooleanExtra("isgold", false);
        isOver = getIntent().getBooleanExtra("isover", false);
        qdp = getIntent().getStringExtra("qdp");
        tjp = getIntent().getStringExtra("tjp");
        bookid = getIntent().getStringExtra("bookId");
        bookname = getIntent().getStringExtra("bookName");
        book_name.setText(bookname);
        tj_num.setText(tjp);
        qd_num.setText(qdp);
        ranking.setText(goldNum);
        String bookname;
        bookname = getIntent().getStringExtra("bookName");
        back_tv.setText(bookname);
        if (TextUtils.isEmpty(userGoldMatchCount)) {
            userGoldMatch = Integer.parseInt(userGoldMatchCount);
        }else {
            userGoldMatch = 0;
        }
        userSupport = Integer.parseInt(userSupportCount);

        // if(userSupportCount.equals("") || userSupportCount == null) {
        if (TextUtils.isEmpty(userSupportCount)) {
            vote_button_tj.setText("投票(" + 0 + "张)");
        }else {
            vote_button_tj.setText("投票(" + userSupportCount + "张)");
        }

        // if(userGoldMatchCount.equals("") || userGoldMatchCount == null) {
        if (TextUtils.isEmpty(userGoldMatchCount)) {
            vote_button_gold.setText("投票(" + 0 + "张)");
        }else {
            vote_button_gold.setText("投票(" + userGoldMatchCount + "张)");
        }
        // if (isOver) {
        // qdp_rel.setVisibility(View.GONE);
        // } else {
        // qdp_rel.setVisibility(View.VISIBLE);
        // }

        //显示黄金联赛注释
        //        if(isGold) {
        //            gold_rel.setVisibility(View.VISIBLE);
        //        }else {
        //            gold_rel.setVisibility(View.GONE);
        //        }
    }

    @Override
    protected void setListener() {
        back.setOnClickListener(this);
        vote_button_gold.setOnClickListener(this);
        vote_button_tj.setOnClickListener(this);
        vote_button_qd.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                SPUtil.putInt("tj_ticket", count);
                SPUtil.putInt("userGoldMatch", userGoldMatch);
                SPUtil.putInt("userSupport", userSupport);
                finish();
                break;
            case R.id.vote_button_gold:
                type = "huangjinliansai";
                if (userGoldMatchCount.equals("0")) {
                    ToastMgr.showShortToast("可用票数为0");
                }else {

                    send();
                }
                break;
            case R.id.vote_button_tj:
                type = "tuijian";
                if (userSupportCount.equals("0")) {
                    ToastMgr.showShortToast("可用票数为0");
                }else {

                    send();
                }
                break;
            case R.id.vote_button_qd:
                type = "qidai";
                send();
                break;

            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {

        super.onBackPressed();
        SPUtil.putInt("tj_ticket", count);
        SPUtil.putInt("userGoldMatch", userGoldMatch);
        SPUtil.putInt("userSupport", userSupport);
        finish();
    }

    private void send() {

        HashMap<String, String> map = new HashMap<String, String>();
        map.put("type", type);
        map.put("bookId", bookid);
        map.put("sd", SPUtil.getString("token", ""));

        RequestListener requestListener = new RequestListener() {
            @Override
            public void onPreExecute() { KLog.e("---------volley onPreExecute!!!"); }

            @Override
            public void onSuccess(HttpResponse httpResponse) {
                try {
                    JSONObject obj = new JSONObject(httpResponse.responseContent);
                    String result = obj.getString("result");
                    if (Constants.RESULT_OK.equals(result)) {
                        ToastMgr.showShortToast("投票成功");

                        if ("qidai".equals(type)) {
                            qd_num.setText((Integer.parseInt(qdp) + 1) + "");

                        }else if ("tuijian".equals(type)) {
                            tj_num.setText((Integer.parseInt(tjp) + 1) + "");
                            userSupport = userSupport - 1;
                            SPUtil.putBoolean("ticket_refresh", true);
                            vote_button_tj.setText("投票(可用" + userSupport + ")");
                            tjp = Integer.parseInt(tjp) + 1 + "";

                        }else if ("huangjinliansai".equals(type)) {
                            ranking.setText((Integer.parseInt(goldNum) + 1) + "");
                            userGoldMatch = userGoldMatch - 1;
                            SPUtil.putBoolean("ticket_refresh", true);
                            vote_button_gold.setText("投票(可用" + userGoldMatch + ")");
                            goldNum = Integer.parseInt(goldNum) + 1 + "";
                        }
                        count = count + 1;
                    }else {
                        ToastMgr.showShortToast(obj.getString("error_msg"));
                    }
                }catch (JSONException e) {
                    ToastMgr.showShortToast(R.string.laikan_data_load_failed);
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(VolleyErrorHandler errorHandler) {
                ToastMgr.showShortToast(R.string.laikan_network_anomaly);
                KLog.e("网络不给力，请重试。63");
            }

            @Override
            public void onFinish() {

            }
        };
        Requester requester = new Requester();
        requester.setUrl(APIProtocol.laikan_goden);
        requester.setMapBody(map);
        HttpApiRequestEngine.getInstance().httpPostRequest(requester, requestListener);

//        HttpApiRequestEngine.getInstance().httpPostRequest(APIProtocol.laikan_goden, map, null, null, requestListener);
    }

}
